#pragma once
#include <iostream>
using namespace std;
namespace hql
{
	template<class T>
	struct ListNode
	{
		ListNode<T>* _next;
		ListNode<T>* _prev;
		T _data;

		ListNode(const T& data = T())
			: _next(nullptr)
			, _prev(nullptr)
			, _data(data)
		{}
	};
	template<class T, class Ref, class Ptr>
	struct __list_iterator
	{
		typedef ListNode<T> Node;
		typedef __list_iterator<T, Ref, Ptr> self;
		Node* _node;
		__list_iterator(Node* x)
			:_node(x) {}
		Ref operator*()
		{
			return _node->_data;
		}
		Ptr operator->()
		{
			return &_node->_data;
		}
		self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		self operator++(int)
		{
			self tmp(*this);
			_node = _node->_next;
			return tmp;
		}
		self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}
		self operator--(int)
		{
			__list_iterator<T, Ref> tmp(*this);
			_node = _node->_prev;
			return tmp;
		}
		bool operator!=(const self& it)
		{
			return _node != it._node;
		}
		bool operator==(const self& it)
		{
			return _node == it._node;
		}


	};
	template<class T>
	class list
	{
	public:
		typedef ListNode<T> Node;
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator<T, const T&, const T*> const_iterator;


		list()
		{
			_head = new Node();
			_head->_next = _head;
			_head->_prev = _head;
		}
		void push_back(const T& data)
		{
			Node* tail = _head->_prev;
			Node* newNode = new Node(data);
			tail->_next = newNode;
			newNode->_prev = tail;

			newNode->_next = _head;
			_head->_prev = newNode;
		}
		iterator begin()
		{
			return iterator(_head->_next);
		}
		iterator end()
		{
			return iterator(_head);
		}
		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}
		const_iterator end() const
		{
			return const_iterator(_head);
		}

	private:
		Node* _head;
	};
	void print_list(const list<int>& lt)
	{
		list<int>::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << endl;
			it++;
		}
	}
	void test_list()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			*it = 10;
			cout << *it << " ";
			++it;
		}
		std::cout << endl;
	}
	struct Date
	{
		int _year;
		int _month;
		int _day;

		Date(int year = 1, int month = 1, int day = 1)
			: _year(year)
			, _month(month)
			, _day(day)
		{}
	};
	void test_list2()
	{
		list<Date> lt;
		lt.push_back(Date(2022, 10, 25));
		lt.push_back(Date(2022, 10, 26));
		lt.push_back(Date(2022, 10, 27));

		list<Date>::iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << it->_year << "/" << it->_month << "/" << it->_day << endl;;
			++it;
		}
		std::cout << endl;
	}
}